package model;

import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class PengendaliPenerimaSuratDokumen {

    public static ArrayList<Surat> DAFTAR_SURAT = new ArrayList<Surat>();
    public static ArrayList<Dokument> DAFTAR_DOKUMEN = new ArrayList<Dokument>();

    public static void tambahSurat(Surat inSurat) {
        DAFTAR_SURAT.add(inSurat);
    }

    public static void tambahDokumen(Dokument inDokumen) {
        DAFTAR_DOKUMEN.add(inDokumen);
    }

    public static String bacaDaftarSurat() {
        if (DAFTAR_SURAT.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_SURAT.size(); i++) {
//                hasil += DAFTAR_SURAT.get(i).getNomorSurat();
//                Dokument dok = DAFTAR_DOKUMEN.get(i);
                Surat srt = DAFTAR_SURAT.get(i);
                hasil += "\nNomor Surat :" + srt.getNomorSurat() + "\nPerihal :" + srt.getPerihal() + "\nAsal Surat :" + srt.getAsalSurat()
                        + "\nLetak Surat :" + srt.getLetakSurat() + "\nPenerima Surat :" + srt.getPenerimaSurat() + "\nTanggal Terima :"
                        + srt.getTanggalTerima() + "\nPenerima Surat :" + srt.getPetugasPenerima();
            }
            return hasil;
        } else {
            return "kosong";
        }
    }

    public static String bacaDaftarDokumen() {
        if (DAFTAR_DOKUMEN.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_DOKUMEN.size(); i++) {
//                hasil += DAFTAR_DOKUMEN.get(i).getNomorDokument();
                Dokument dok = DAFTAR_DOKUMEN.get(i);
                hasil += "\nNomor Dokumen :" + dok.getNomorDokument() + "\nPerihal :" + dok.getPerihal() + "\nAsal Dokumen :" + dok.getAsalDokumen()
                        + "\nLetak Dokumen :" + dok.getLetakDokumen() + "\nPenerima Dokumen :" + dok.getPenerimaDokumen() + "\nTanggal Terima :"
                        + dok.getTanggalTerima() + "\nPenerima Dokumen :" + dok.getPetugasPenerima();
            }
            return hasil;
        } else {
            return "kosong";
        }
    }

    public static void simpanFileSurat() {
        FileOutputStream outStream = null;
        if (DAFTAR_SURAT.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Surat Kosong");
        } else {
            try {
                File outFile = new File("surat.txt");
                outStream = new FileOutputStream(outFile);
                ObjectOutputStream outObject = new ObjectOutputStream(outStream);
                outObject.writeObject(DAFTAR_SURAT);
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(PengendaliPenerimaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(PengendaliPenerimaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void simpanFileDokumen() {
        FileOutputStream outStream = null;
        if (DAFTAR_DOKUMEN.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Dokumen Kosong");
        } else {
            try {
                File outFile = new File("dokumen.txt");
                outStream = new FileOutputStream(outFile);
                ObjectOutputStream outObject = new ObjectOutputStream(outStream);
                outObject.writeObject(DAFTAR_DOKUMEN);
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(PengendaliPenerimaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(PengendaliPenerimaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void bacaFileSurat() {
        try {
            FileInputStream inStream = null;
            File inFile = new File("surat.txt");
            inStream = new FileInputStream(inFile);
            ObjectInputStream inObject = new ObjectInputStream(inStream);
            DAFTAR_SURAT = (ArrayList<Surat>) inObject.readObject();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PengendaliPenerimaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PengendaliPenerimaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PengendaliPenerimaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void bacaFileDokumen() {
        try {
            FileInputStream inStream = null;
            File inFile = new File("dokumen.txt");
            inStream = new FileInputStream(inFile);
            ObjectInputStream inObject = new ObjectInputStream(inStream);
            DAFTAR_DOKUMEN = (ArrayList<Dokument>) inObject.readObject();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PengendaliPenerimaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PengendaliPenerimaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PengendaliPenerimaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
//
//     public static String bacaDaftarDokumen (){
//        if(DAFTAR_DOKUMEN.isEmpty()== false){
//            String hasil=new String();
//            for(int i=0; i<DAFTAR_DOKUMEN.size(); i++){
//                hasil += DAFTAR_DOKUMEN.get(i).getNomorDokument() + "\n";
//            }
//            return hasil;
//    } else{
//            return "kosong";
//    }
//}

    public static void tambahTabelSurat(Surat surat) {
        try {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314063", "MHS115314063");
            java.sql.Statement statement = conn.createStatement();
            String sql = "insert into surat (NO_SURAT, perihal, asal_surat, penerima_surat,"
                    + " tanggal_terima, letak_surat, petugas_penerima) values "
                    + "('" + surat.getNomorSurat() + "', '" + surat.getPerihal() + "', '"
                    + surat.getAsalSurat() + "'," + "'" + surat.getPenerimaSurat() + "','"
                    + surat.getLetakSurat() + "','" + "'" + surat.getTanggalTerima() + "','"
                    + "','" + surat.getPetugasPenerima() + "')";
            statement.executeUpdate(sql);
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Surat.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void bacaTabelSurat() {
        DAFTAR_SURAT = new ArrayList<Surat>();
        Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314063", "MHS115314063");
//        Statement statement = conn.createStatement();
        String sql = "select * from surat";
    }

    public static void tambahTabelDokumen(Dokument dokument) {
        try {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314063", "MHS115314063");
            java.sql.Statement statement = conn.createStatement();
            String sql = "insert into dokumen (no_dokumen, perihal, asal_dokumen, penerima_dokumen,"
                    + " tanggal_terima, letak_dokumen, petugas_penerima) values " + "('"
                    + dokument.getNomorDokument() + "', '" + dokument.getPerihal() + "', '"
                    + dokument.getAsalDokumen() + "', '" + dokument.getPenerimaDokumen() + "', '"
                    + dokument.getLetakDokumen() + "', '" + dokument.getTanggalTerima() + "', '"
                    + "', '" + dokument.getPetugasPenerima() + "')";
            statement.executeUpdate(sql);
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Surat.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void bacaTabelDokumen() {
        DAFTAR_DOKUMEN = new ArrayList<Dokument>();
        Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314063", "MHS115314063");
//        Statement statement = conn.createStatement();
        String sql = "select * from dokumen";
    }

    public static void simpanTabelSurat() {
        if (DAFTAR_SURAT.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Surat Kosong");
        } else {
            try {
//                Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314063", "MHS115314063");
                Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314063", "MHS115314063");
                Statement statement = conn.createStatement();
                int i = 0;
                while (i < DAFTAR_SURAT.size()) {
                    String sql = "insert into surat (NO_SURAT, perihal, asal_surat, penerima_surat, "
                            + "tanggal_terima, letak_surat, petugas_penerima)"
                            + " values ('" + DAFTAR_SURAT.get(i).getNomorSurat() + "','"
                            + DAFTAR_SURAT.get(i).getPerihal() + "','"
                            + DAFTAR_SURAT.get(i).getAsalSurat() + "','"
                            + DAFTAR_SURAT.get(i).getPenerimaSurat() + "','"
                            + DAFTAR_SURAT.get(i).getTanggalTerima() + "','"
                            + DAFTAR_SURAT.get(i).getLetakSurat() + "','"
                            + DAFTAR_SURAT.get(i).getPetugasPenerima() + "')";
                    statement.executeUpdate(sql);
                    i++;
                }
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Surat.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void simpanTabelDokumen() {
        if (DAFTAR_DOKUMEN.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Dokumen Kosong");
        } else {
            try {
//                Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314063", "MHS115314063");
                Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314063", "MHS115314063");
                Statement statement = conn.createStatement();
                int i = 0;
                while (i < DAFTAR_DOKUMEN.size()) {
                    String sql = "insert into dokumen (no_dokumen, perihal, asal_dokumen, penerima_dokumen,"
                            + " tanggal_terima, letak_dokumen, petugas_penerima)"
                            + " values ('" + DAFTAR_DOKUMEN.get(i).getNomorDokument() + "', '"
                            + DAFTAR_DOKUMEN.get(i).getPerihal() + "', '"
                            + DAFTAR_DOKUMEN.get(i).getAsalDokumen() + "', '"
                            + DAFTAR_DOKUMEN.get(i).getPenerimaDokumen() + "', '"
                            + DAFTAR_DOKUMEN.get(i).getTanggalTerima() + "', '"
                            + DAFTAR_DOKUMEN.get(i).getLetakDokumen() + "', '"
                            + DAFTAR_DOKUMEN.get(i).getPetugasPenerima() + "')";
                    statement.executeUpdate(sql);
                    i++;
                }
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Dokument.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
